
import {
  Component,
  OnInit,
  ViewEncapsulation,
  AfterViewInit,
  ViewChild,
  ElementRef,
  NgZone,
  OnDestroy,
  Injector,
  Input,
  ViewContainerRef,
  ComponentRef,
  Type,
  ComponentFactory
} from '@angular/core';

import {AbmComponent} from 'angular-baidu-maps';

import {AppComponentBase} from '@shared/app-component-base';

import { NzModalSubject, NzModalService, NzMessageService } from 'ng-zorro-antd';

import { FileUploader } from 'ng2-file-upload';

import { AppConsts } from '@shared/AppConsts';

import {  FileDto } from '@shared/service-proxies/service-proxies';


@Component({
  selector: 'app-upload-model',
  templateUrl: './upload-model.component.html',
  styles: []
})
export class UploadModelComponent extends AppComponentBase implements OnInit {


  uploader: FileUploader = new FileUploader({
    url: AppConsts.remoteServiceBaseUrl + '/File/UploadTempFile',
    isHTML5: true,
    method: 'POST',
    autoUpload:true
});

@Input()
title?:string;

@Input()
action: (file: FileDto,host:any,riverType:string,excelFileType:string ) => Promise<any>;

@Input()
host:any;

riverType:string;
excelFileType:string;

constructor(injector : Injector,
    private subject: NzModalSubject) {
    super(injector);

    let self = this;

    this.uploader.onAfterAddingFile = (file) => {
        file.withCredentials = false;
    };

    this.uploader.onCompleteItem=(f,r,s,h)=>{

        let res = JSON.parse(r);
        console.log(res);
        if (!res.success) {
            this.notify.error('上传文件失败');
            return;
        }

        let file = res.result;


        if (self.action == undefined) {

            self.save();
            return;
        }

        self.action(file,self.host,self.riverType,self.excelFileType).then(
            () => {
                self.save();
            }
        );
       /* */
    };
}


save(): void {
    this.subject.destroy('onOk');
}

close(): void {

    this.subject.destroy();
}

ngOnInit() {
  this.riverType = '1';
  this.excelFileType = 'heliu';
    // this.componentRef =  this.container.createComponent(this.compFactory);
    // this.container.createComponent();
}



}
